1
|
|
View Code Duplication |
var chai = require('chai'); |
|
|
|
|
2
|
|
|
var chaiHttp = require('chai-http'); |
3
|
|
|
var should = chai.should(); |
4
|
|
|
|
5
|
|
|
var server = require('../../index'); |
6
|
|
|
var Beer = require('../../models/beer.model'); |
7
|
|
|
var Brewery = require('../../models/brewery.model'); |
8
|
|
|
var BeerData = require('../../beers.json'); |
9
|
|
|
var BreweryData = require('../../breweries.json'); |
10
|
|
|
|
11
|
|
|
chai.use(chaiHttp); |
12
|
|
|
|
13
|
|
|
describe('GraphQL Beer (queries)', function() { |
14
|
|
|
|
15
|
|
|
beforeEach(function(done){ |
16
|
|
|
BreweryData.forEach(function (aBeer) { |
17
|
|
|
var newBrewery = new Brewery(aBeer); |
18
|
|
|
newBrewery.save(); |
19
|
|
|
}); |
20
|
|
|
BeerData.forEach(function (aBeer) { |
21
|
|
|
var newBeer = new Beer(aBeer); |
22
|
|
|
newBeer.save(); |
23
|
|
|
}); |
24
|
|
|
done(); |
25
|
|
|
}); |
26
|
|
|
|
27
|
|
|
afterEach(function(done){ |
28
|
|
|
Beer.collection.drop(); |
29
|
|
|
Brewery.collection.drop(); |
30
|
|
|
done(); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
it('should display all the beers', function(done) { |
34
|
|
|
var graphqlQuery = ` |
35
|
|
|
{ |
36
|
|
|
beers { |
37
|
|
|
name |
38
|
|
|
alcohol |
39
|
|
|
description |
40
|
|
|
} |
41
|
|
|
}`; |
42
|
|
|
|
43
|
|
|
chai.request(server) |
44
|
|
|
.post('/graphql') |
45
|
|
|
.send({query: graphqlQuery}) |
46
|
|
|
.end(function(err, res){ |
47
|
|
|
res.should.have.status(200); |
48
|
|
|
res.should.be.a.json; |
|
|
|
|
49
|
|
|
res.body['data']['beers'].should.be.an('array'); |
50
|
|
|
res.body['data']['beers'][0].should.have.property('name'); |
51
|
|
|
res.body['data']['beers'][0].should.have.property('alcohol'); |
52
|
|
|
done(); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
it('should display only beers from Duvel Brewery', function(done) { |
57
|
|
|
var graphqlQuery = ` |
58
|
|
|
{ |
59
|
|
|
beers(brewery: "duvel") { |
60
|
|
|
name |
61
|
|
|
alcohol |
62
|
|
|
brewery { |
63
|
|
|
name |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
}`; |
67
|
|
|
|
68
|
|
|
chai.request(server) |
69
|
|
|
.post('/graphql') |
70
|
|
|
.send({query: graphqlQuery}) |
71
|
|
|
.end(function(err, res){ |
72
|
|
|
res.should.have.status(200); |
73
|
|
|
res.should.be.a.json; |
|
|
|
|
74
|
|
|
for (var beer of res.body['data']['beers']) { |
75
|
|
|
beer.brewery.name.should.equal('Duvel Moortgat'); |
76
|
|
|
} |
77
|
|
|
done(); |
78
|
|
|
}); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
it('should display beers ordered by alcohol', function(done) { |
82
|
|
|
var graphqlQuery = ` |
83
|
|
|
{ |
84
|
|
|
beers(orderBy: ALCOHOL) { |
85
|
|
|
name |
86
|
|
|
alcohol |
87
|
|
|
} |
88
|
|
|
}`; |
89
|
|
|
|
90
|
|
|
chai.request(server) |
91
|
|
|
.post('/graphql') |
92
|
|
|
.send({query: graphqlQuery}) |
93
|
|
|
.end(function(err, res){ |
94
|
|
|
res.should.have.status(200); |
95
|
|
|
res.should.be.a.json; |
|
|
|
|
96
|
|
|
var previousAlcohol = 1; |
97
|
|
|
for (var beer of res.body['data']['beers']) { |
98
|
|
|
beer.alcohol.should.be.at.least(previousAlcohol); |
99
|
|
|
previousAlcohol = beer.alcohol; |
100
|
|
|
} |
101
|
|
|
done(); |
102
|
|
|
}); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
it('should display only beers from Duvel Brewery and ordered by alcohol', function(done) { |
106
|
|
|
var graphqlQuery = ` |
107
|
|
|
{ |
108
|
|
|
beers(brewery: "Duvel", orderBy: ALCOHOL) { |
109
|
|
|
name |
110
|
|
|
brewery { |
111
|
|
|
name |
112
|
|
|
} |
113
|
|
|
alcohol |
114
|
|
|
} |
115
|
|
|
}`; |
116
|
|
|
|
117
|
|
|
chai.request(server) |
118
|
|
|
.post('/graphql') |
119
|
|
|
.send({query: graphqlQuery}) |
120
|
|
|
.end(function(err, res){ |
121
|
|
|
res.should.have.status(200); |
122
|
|
|
res.should.be.a.json; |
|
|
|
|
123
|
|
|
var previousAlcohol = 1; |
124
|
|
|
for (var beer of res.body['data']['beers']) { |
125
|
|
|
beer.brewery.name.should.equal('Duvel Moortgat'); |
126
|
|
|
beer.alcohol.should.be.at.least(previousAlcohol); |
127
|
|
|
previousAlcohol = beer.alcohol; |
128
|
|
|
} |
129
|
|
|
done(); |
130
|
|
|
}); |
131
|
|
|
}); |
132
|
|
|
|
133
|
|
|
it('should display a SINGLE beer', function(done) { |
134
|
|
|
var newBeer = new Beer({ |
135
|
|
|
name: 'Custom Beer', |
136
|
|
|
brewery: 'Custom Brewery', |
137
|
|
|
alcohol: 9.5, |
138
|
|
|
description: 'miam miam' |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
newBeer.save(function(err, data) { |
142
|
|
|
|
143
|
|
|
var graphqlQuery = ` |
144
|
|
|
{ |
145
|
|
|
beer(id: "${data._id}") { |
146
|
|
|
name |
147
|
|
|
} |
148
|
|
|
}`; |
149
|
|
|
|
150
|
|
|
chai.request(server) |
151
|
|
|
.post('/graphql') |
152
|
|
|
.send({query: graphqlQuery}) |
153
|
|
|
.end(function(err, res){ |
154
|
|
|
res.should.have.status(200); |
155
|
|
|
res.should.be.a.json; |
|
|
|
|
156
|
|
|
res.body.should.be.an('object'); |
157
|
|
|
res.body['data']['beer']['name'].should.equal('Custom Beer'); |
158
|
|
|
done(); |
159
|
|
|
}); |
160
|
|
|
}); |
161
|
|
|
}); |
162
|
|
|
}); |
163
|
|
|
|